/**
 *  问题1： 查找指定字符串的命令代码的自动生成问题
 * 
 *  要求： 给出一个字符串（暂时定为二进制数字字符串），生成一个能够自动查找指定字符串的功能代码的方法
 * 
 * 
 *    11100..0  length = n;
 * 
 *    q0 = if b => no insert , R , turn q(n+2) 
 *    q0 = if 0 => no insert , R , turn q(0+1) 
 *    q0 = if 1 => no insert , R , turn q0 
 * 
 *    q1 = if b => no insert , R , turn q(n+2) 
 *    q1 = if 0 => no insert , R , turn q(1+1) 
 *    q1 = if 1 => no insert , R , turn q0 
 * 
 *    qn = if b => no insert , R , turn q(n+2) 
 *    qn = if 0 => no insert , R , turn q(n+1) 
 *    qn = if 1 => no insert , R , turn q0 
 * 
 *  
 *    q(n+1) = if b => no insert , H , turn q0 
 *    q(n+1) = if 0 => no insert , H , turn q0 
 *    q(n+1) = if 1 => no insert , H , turn q0
 * 
 *    q(n+2) = if b => no insert , H , turn q0 
 *    q(n+2) = if 0 => no insert , H , turn q0 
 *    q(n+2) = if 1 => no insert , H , turn q0
 * 
 * 
 *    命令状态解释：
 *       let order = ["q0", -1, -2, 1, "q0"]  
 *       order[0] : 当前状态为 q0 时
 *              可选：  q0  -   q(n+2)
 *       order[1] : 读取头读到的纸带信息
 *              可选：  
 *                    -1: 空白
 *                     0: 0
 *                     1: 1
 *       order[2] : 读取头写到的纸带信息
 *              可选：  
 *                    -2: 不写入
 *                    -1: 空白
 *                     0: 0
 *                     1: 1
 *       order[3] : 读取头移动状态
 *              可选：  
 *                    -2: 停机
 *                    -1: 左移动1格
 *                     0: 不移动
 *                     1: 左移动1格
 *       order[4] : 设置命令状态，范围同 order[0]
 * 
 */

  // filter  input: "00000"    output: ["0","0","0","0","0"]
  function filterData(input) {
    return input.split("");
  }
  // 查找指定连续的字符集合
  function find(input) {
    let arr = filterData(input);
    let len = arr.length;
    let orderObj = {};
    orderObj.baseInfo = {
      input,
      n: input.length
    };
    arr.forEach((element, index) => {
      orderObj[`q${index}`] = {};
      orderObj[`q${index}`]["-1"] = [`q${index}`, -1, -2, 1, `q${len + 1}`];
      if (element === "0") {
        orderObj[`q${index}`]["0"] = [`q${index}`, 0, -2, 1, `q${index + 1}`];
        orderObj[`q${index}`]["1"] = [`q${index}`, 1, -2, 1, `q${-index}`];
      } else if (element === "1") {
        orderObj[`q${index}`]["0"] = [`q${index}`, 0, -2, 1, `q${-index}`];
        orderObj[`q${index}`]["1"] = [`q${index}`, 1, -2, 1, `q${index + 1}`];
      }
      // if (index > 0) {
        orderObj[`q${-index-1}`] = {};
        orderObj[`q${-index-1}`]["-1"] = [`q${-index-1}`, -1, -2, -1, `q${(-index)}`];
        orderObj[`q${-index-1}`]["0"] = [`q${-index-1}`, 0, -2, -1, `q${(-index)}`];
        orderObj[`q${-index-1}`]["1"] = [`q${-index-1}`, 1, -2, -1, `q${(-index)}`];
      // }
    });
    // return true
    orderObj[`q${len}`] = {};
    orderObj[`q${len}`]["-1"] = [`q${len}`, -1, -2, "H", `q${len}`];
    orderObj[`q${len}`]["0"] = [`q${len}`, 0, -2, "H", `q${len}`];
    orderObj[`q${len}`]["1"] = [`q${len}`, 1, -2, "H", `q${len}`];
    orderObj.true = orderObj[`q${len}`];
    // return false
    orderObj[`q${len + 1}`] = {};
    orderObj[`q${len + 1}`]["-1"] = [`q${len + 1}`, -1, -2, "H", `q${len + 1}`];
    orderObj[`q${len + 1}`]["0"] = [`q${len + 1}`, 0, -2, "H", `q${len + 1}`];
    orderObj[`q${len + 1}`]["1"] = [`q${len + 1}`, 1, -2, "H", `q${len + 1}`];
    orderObj.false = orderObj[`q${len + 1}`];


    // get array
    // arr0 = orderObj;
    let arr0 = [];
    for (let key in orderObj) {
      if (key !== "baseInfo" && key !== "true" && key !== "false") {
        arr0.push(orderObj[key]["-1"], orderObj[key]["0"], orderObj[key]["1"]);
      }
    }
    // arr0.map(ele => {return JSON.stringify(ele);});
    console.log(`查找 ${input} 自动生成的机器指令`,arr0);
    return orderObj;
  }

  export default {
    find
  };